<?php
namespace Api\Controller;
use Think\Controller;
class PersonController extends CommonController {
	public function newaddr(){
		$addr_id = I ('get.addr_id');
		$user_nickname = I ('get.user_nickname');
		$mobile_no = I ('get.mobile_no');
		$province = I ('get.province');
		$city = I ('get.city');
		$area = I ('get.area');
		$addr = I ('get.addr');
		$Address = M ("Address");
		//1.需要判断值是否为空，为空不能提交
		//2.地址相同不能重复添加
		//3.符合条件才能进入数据库
		$data = array(
			'user_nickname' => $user_nickname,
			'mobile_no' => $mobile_no,
			'province' => $province,
			'city' => $city,
			'area' => $area,
			'addr' => $addr,
			'addtime' => time(),
			'user_id' => $_SESSION['user_id']
		);
		if( $addr_id != ' '  ){
			$Address->data($data)->where("addr_id={$addr_id}")->save($data);
			$this->response(true,1,"修改成功",true);
			var_dump(1111);
		}else{
			$res = $Address->add($data);
			if( $res == false){
				$this->response(false,0,"未知错误",true);
			}else{
				$this->response(true,2,"添加成功",true);
				var_dump(2222);
			}
		}
		
		
	}
   public function show(){
		$Address = M ("Address");
		$res = $Address->where("user_id='{$_SESSION['user_id']}'")->select();
		if( $res == false){
			$this->response(false,0,"暂无收货地址",true);
		}else{
			$this->response(true,1,$res,true);
		}   
	}
	public function moren(){
		$user_id = $_SESSION['user_id'];
		$addr_id = I ("get.addr_id");
		$Address = M ("Address");
		//$data =array( 'status' => 2);
		$res = $Address->where("user_id='{$user_id}' AND addr_id != {$addr_id}")->setField("status",'0');
		//$res = $Address->data($data)->where("user_id={$user_id} AND addr_id != {$addr_id}")->save();
		$ret = $Address->where("user_id='{$user_id}' and addr_id='{$addr_id}'")->setField("status",'1');
		if( $res == false && $ret == false){
			$this->response(false,0,"失败",true);
		}else{
			$this->response(true,1,$res,true);
		} 
	}
	public function showmoren(){
		$Address = M ("Address");
		$res = $Address->where("user_id='{$_SESSION['user_id']}' AND status='1'")->find();
		if( $res == false){
			$res = $Address->where("user_id='{$_SESSION['user_id']}'")->find();
			$data = array(
				'status' => 1
			);
			$res = $Address->data($data)->where("addr_id={$res['addr_id']}")->save();
			if($res != false){
				$this->response(true,0,$res,true);
			}else{
				$this->response(false,0,"未知错误",true);
			}
			
		}else{
			$this->response(true,1,$res,true);
		} 
	}
	public function delClick(){
		$addr_id = I ("get.addr_id");
		$Address = M ("Address");
		$res = $Address->where("addr_id={$addr_id}")->delete();
		if( $res == false){
			$this->response(false,0,"删除失败",true);
		}else{
			$this->response(true,1,"删除成功",true);
		} 
	}
}
